package com.tongji.housediy.view
{
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.GridContainerEvent;
	import com.tongji.housediy.events.commonEvent.MoveCamera3D;
	import com.tongji.housediy.model.ViewControlModel;
	import com.tongji.housediy.utils.ConstVariableInventory;
	
	import flash.events.MouseEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class HorizontalZoomMediator extends Mediator
	{
		[Inject]
		public var view:HorizontalZoomScrollbar;
		
		[Inject]
		public var viewControlModel:ViewControlModel;
		
		private var tempValue:Number;
		
		override public function onRegister():void
		{
			view.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbMouseDownHandler);
			
			this.addContextListener(GridContainerEvent.UPDATE_SCROLL_VALUE, updateScrollValueHandler, GridContainerEvent);
		}
		
		private function thumbMouseDownHandler(event:MouseEvent):void
		{
			view.thumb.removeEventListener(MouseEvent.MOUSE_DOWN, thumbMouseDownHandler);
			view.stage.addEventListener(MouseEvent.MOUSE_UP, thumbMouseUpHandler);
			
			tempValue = view.hostComponent.value;
		}
		
		private function thumbMouseUpHandler(event:MouseEvent):void
		{
			view.stage.removeEventListener(MouseEvent.MOUSE_UP, thumbMouseUpHandler);
			view.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbMouseDownHandler);
			
			if (view.hostComponent.value != tempValue)
			{
				viewControlModel.dimensionValue = view.hostComponent.value;
				viewControlModel.tempDimensionValue = tempValue;
				if (viewControlModel.dimensionIndex == 0)
					dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_GRID_SCALE, view.hostComponent.value - tempValue));
				else
					dispatch(new MoveCamera3D(MoveCamera3D.CAMERA_SELECT,view.hostComponent.value - 5));
			}
		}
		
		private function updateScrollValueHandler(event:GridContainerEvent):void
		{
			var value:int = view.hostComponent.value + event.scaleMultiple;
			if (value >= ConstVariableInventory.SCROLLBAR_MIN_VALUE && value <= ConstVariableInventory.SCROLLBAR_MAX_VALUE)
			{
				view.hostComponent.value += event.scaleMultiple;
				dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_GRID_SCALE, event.scaleMultiple, true));
			}
		}
	}
}